package com.zadtek.shopmall.activities;

import android.support.annotation.Nullable;
import android.support.v4.util.ArrayMap;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;
import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsListActivity;
import com.zadtek.shopmall.bean.BrowingHistoryBean;
import com.zadtek.shopmall.bean.BrowingProductItem;
import com.zadtek.shopmall.bean.HomeBaseBean;
import com.zadtek.shopmall.bean.HomeShopBean;
import com.zadtek.shopmall.bean.OperateResponseBean;
import com.zadtek.shopmall.net.BaseObserver;
import com.zadtek.shopmall.net.BaseResponse;
import com.zadtek.shopmall.net.NetTool;
import com.zadtek.shopmall.other.Const;
import com.zadtek.shopmall.utils.ToastUtil;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class BrowingHistoryActivity extends AbsListActivity {
    private BrowingAdapter browingAdapter;
    private List<BrowingProductItem> data = new LinkedList<>();

    private int page =1 ;
    private TextView tvClear,tvBrowingNum;
    private int pagesize = 10;

    @Override
    protected String getTopTitle() {
        return "浏览记录";
    }

    @Override
    public BaseQuickAdapter getAdapter() {
        browingAdapter = new BrowingAdapter(R.layout.collect_item,data);
        browingAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()){
                    case R.id.iv_btn_delete:
                        ToastUtil.showToast("删除！");
                        clearRequest(data.get(position).getGoods_id());
                        break;
                }
            }
        });
        //View v = getHeaderView();
        //browingAdapter.addHeaderView(v);
        return browingAdapter;
    }

    @Override
    public void loadMore() {
        requestData();
    }

    @Override
    protected void requestData() {
//        BrowingHistoryBean browingHistoryBean = new BrowingHistoryBean();
//        browingHistoryBean.setTotalNum(100);
//        List<BrowingProductItem> items = new LinkedList<>();
//        BrowingProductItem indexProductItem = new BrowingProductItem();
//        indexProductItem.setProductPrice("￥20.00");
//        indexProductItem.setProductTitle("美的（Midea）电水壶双层隔热无缝全钢电热水壶WH415E2g（烧水壶)");
//        indexProductItem.setProductImgUrl("http://tc.zadtek.com/aaa/images/201512/thumb_img/471_thumb_G_1451087895685.jpg");
//        indexProductItem.setProductId(12);
//        items.add(indexProductItem);
//        items.add(indexProductItem);
//        items.add(indexProductItem);
//        browingHistoryBean.setProductItems(items);
//        BaseResponse<BrowingHistoryBean> response = new BaseResponse<>();
//        response.setData(browingHistoryBean);
//        response.setCode(0);
//        response.setMessage("");
//        Gson gson = new Gson();
//        String s = gson.toJson(response);

        Map<String, Object> param = NetTool.newParams();
//        final Map<String, Object> param = new ArrayMap<String, Object>();
//        param.put("uid", 10);
//        param.put("token", Const.token);
        param.put("page",page);
        param.put("pagesize",pagesize);
        NetTool.getApi().getBrowingHistory(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<List<BrowingProductItem>>>() {
                    @Override
                    public void onData(BaseResponse<List<BrowingProductItem>> value) {
                        if(value.getCode() == 0 && value.getData()!=null){
//                            if(page == 1){
//                                tvBrowingNum.setText("浏览条数 "+value.getData().getTotalNum()+" 条");
//                            }
                            if(value.getData().size() != 0){
                                data.addAll(value.getData());
                                browingAdapter.notifyDataSetChanged();
                                browingAdapter.loadMoreComplete();
                                page++;
                            }else{
                                browingAdapter.loadMoreEnd();
                            }
                        }
                    }
                });
    }

    @Override
    public void OnItemClick(int position) {
        ToastUtil.showToast("点击了+"+position);
        HomeBaseBean baseBean = new HomeBaseBean();
        baseBean.setType(Const.PRODUCT);
        baseBean.setId(data.get(position).getGoods_id());
        Const.jumpHandle(this,baseBean);
    }

    public View getHeaderView() {
        View v = getLayoutInflater().inflate(R.layout.browing_history_header,null);
        tvBrowingNum = (TextView)v.findViewById(R.id.tv_browing_num);
        tvClear = (TextView)v.findViewById(R.id.tv_clear);
        tvClear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearRequest(-1);
            }
        });
        return v;
    }

    private void clearRequest(int productId) {
        Map<String, Object> param = NetTool.newParams();
        if(productId == -1){
            param.put("deleteAll",true);
        }else{
            param.put("goods_id",productId);
        }
        NetTool.getApi().delBrowingHistory(param)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<OperateResponseBean>>() {
                    @Override
                    public void onData(BaseResponse<OperateResponseBean> value) {
                        if(value.getCode() == 0 && value.getData().isSuccess()){
                            ToastUtil.showToast("操作成功！");
                        }else{
                            ToastUtil.showToast(value.getData().getFailMsg());
                        }
                    }
                });
    }

    private class BrowingAdapter extends BaseQuickAdapter<BrowingProductItem,BaseViewHolder>{

        public BrowingAdapter(int layoutResId, @Nullable List<BrowingProductItem> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, BrowingProductItem item) {
            helper.addOnClickListener(R.id.iv_btn_delete);
            Glide.with(mActivity).load(item.getGoods_img()).placeholder(R.drawable.placeholder).into((ImageView) helper.getView(R.id.iv_product_img));
            helper.setText(R.id.tv_product_desc,item.getGoods_name());
            helper.setText(R.id.tv_product_price,item.getGoods_price());
        }
    }
}
